package com.zmwx.subject.appliacion.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zmwx.subject.appliacion.convert.SubjectCategoryDTOConverter;
import com.zmwx.subject.appliacion.convert.SubjectLabelDTOConverter;
import com.zmwx.subject.appliacion.dto.SubjectCategoryDTO;
import com.zmwx.subject.appliacion.dto.SubjectLabelDTO;
import com.zmwx.subject.common.entity.Result;
import com.zmwx.subject.domain.entity.SubjectCategoryBO;
import com.zmwx.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 刷题分类
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource

    private SubjectCategoryDomainService SubjectCategoryDomainService;
    //新增分类
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("subjectCategoryDTO:{}", subjectCategoryDTO);
        }
        try {
            Preconditions.checkNotNull(subjectCategoryDTO, "subjectCategoryDTO不能为空");
            Preconditions.checkArgument(!(StringUtils.isEmpty(subjectCategoryDTO.getCategoryName())), "分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "父级id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertByToCategoryBO(subjectCategoryDTO);
            if (log.isInfoEnabled()) {
                log.info("subjectCategoryBO:{}", subjectCategoryBO);
            }
            SubjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController-add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());


        }
    }

    /**
     * 获取大类
     * */
    @RequestMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.
                convertByToCategoryBO(subjectCategoryDTO);
        List<SubjectCategoryBO> subjectCategoryBOList = SubjectCategoryDomainService.queryPrimaryCategory(subjectCategoryBO);

        List<SubjectCategoryDTO> ListResult = SubjectCategoryDTOConverter.INSTANCE.convertByToCategorySubjectCategoryDTOList(subjectCategoryBOList);
        if (log.isInfoEnabled()) {
            log.info("ListResult:{}", ListResult);
        }
        return Result.ok(ListResult);
    }

    /**
     * 查询大分类下的分类
     */
    @PostMapping("/getqueryByPrimary")
    public Result<List<SubjectCategoryDTO>> getQueryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("subjectCategoryDTO.getQueryByPrimary:{}", JSON.toJSONString(subjectCategoryDTO));
        }
        Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "父类id不能为空");
        SubjectCategoryBO subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertByToCategoryBO(subjectCategoryDTO);
        List<SubjectCategoryBO> subjectCategoryBOList = SubjectCategoryDomainService.queryCategory(subjectCategoryBo);
        List<SubjectCategoryDTO> ListResult = SubjectCategoryDTOConverter.INSTANCE.convertByToCategorySubjectCategoryDTOList(subjectCategoryBOList);
        return Result.ok(ListResult);
    }

    /**
     * 查询分类及标签 一次性查询出来
     */
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("queryCategoryAndLabel.SubjectCategoryDTO:{}", JSON.toJSONString(subjectCategoryDTO));
        }
        Preconditions.checkNotNull(subjectCategoryDTO.getId(), "父类id不能为空");
        SubjectCategoryBO subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertByToCategoryBO(subjectCategoryDTO);
        List<SubjectCategoryBO> subjectCategoryBOList = SubjectCategoryDomainService.queryCategoryAndLable(subjectCategoryBo);
        List<SubjectCategoryDTO> dtoList = new LinkedList<>();
        subjectCategoryBOList.forEach(bo->{
            SubjectCategoryDTO dto = SubjectCategoryDTOConverter.INSTANCE.convertByToCategoryDTO(bo);
            List<SubjectLabelDTO> labelDTOList = SubjectLabelDTOConverter.INSTANCE.subjectLabelBOConvertToDTO(bo.getLabelBOList());
            dto.setLabelDTOList(labelDTOList);
            dtoList.add(dto);
        });


        return Result.ok(dtoList);
    }

    /**
     * 更新分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("subjectCategoryDTO.update。dto:{}", JSON.toJSONString(subjectCategoryDTO));
        }
        Preconditions.checkNotNull(subjectCategoryDTO.getId(), "大分类id不能为空");
        SubjectCategoryBO subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertByToCategoryBO(subjectCategoryDTO);
        boolean result = SubjectCategoryDomainService.update(subjectCategoryBo);
        return Result.ok(result);
    }
}
